package com.hunyuan.player;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.v4.media.session.MediaControllerCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.SeekBar;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    public static final String EXTRA_CURRENT_MEDIA_DESCRIPTION =
            "com.hunyuan.player.CURRENT_MEDIA_DESCRIPTION";

    private ListView musicList;
    private SeekBar musicSeekBar;
    private ImageButton previous;
    private ImageButton play;
    private ImageButton next;

    private PlayService mPlayService;
    private ArrayAdapter<String> mAdapter;
    private MediaControllerCompat.TransportControls mTransportControls;

    private ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            Toast.makeText(MainActivity.this, "Service链接成功", Toast.LENGTH_SHORT).show();
            PlayService.MyBinder binder = (PlayService.MyBinder) iBinder;
            mPlayService = binder.getPlayService();
            try {
                mTransportControls = new MediaControllerCompat(MainActivity.this, mPlayService.getSessionToken()).getTransportControls();
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {
            mPlayService = null;
            mTransportControls = null;
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        next = (ImageButton) findViewById(R.id.next);
        play = (ImageButton) findViewById(R.id.play);
        previous = (ImageButton) findViewById(R.id.previous);
        musicSeekBar = (SeekBar) findViewById(R.id.musicSeekBar);
        musicList = (ListView) findViewById(R.id.musicList);

        next.setOnClickListener(this);
        play.setOnClickListener(this);
        previous.setOnClickListener(this);

        final List<Music> musicDataList = MusicFactory.buildMusic();

        List<String> musicDesc = new ArrayList<>();
        for (Music music : musicDataList) {
            musicDesc.add(music.getId() + " : " + music.getTitle());
        }

        Intent intent = new Intent(this, PlayService.class);
        bindService(intent, connection, BIND_AUTO_CREATE);

        mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, musicDesc);
        musicList.setAdapter(mAdapter);
        musicList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Toast.makeText(MainActivity.this, musicDataList.get(i).getTitle(), Toast.LENGTH_SHORT).show();
                if (mTransportControls != null)
                    mTransportControls.playFromMediaId(musicDataList.get(i).getId(), null);
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            default:
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbindService(connection);
    }
}
